//
//  Emoticon.swift
//  VVbo
//
//  Created by Mr.Run on 2017/9/7.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit
import YYModel

class Emoticon: NSObject {
    
    var type = false
    var chs:String?
    var png:String?
    var times:Int = 0
    var code:String?{
        didSet{
            
            guard let code = code else {
                return
            }
            
            let scaner = Scanner(string: code)
            var result:UInt32 = 0
            scaner.scanHexInt32(&result)
            
            emoji = String(Character(UnicodeScalar(result)!))
        }
    }
    
    var emoji:String?
    
    var directory: String?
    
    var image: UIImage? {
        
        
        if type {
            return nil
        }
        
        guard let directory = directory,
            let png = png,
            let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil),
            let bundle = Bundle(path: path)
            else {
                return nil
        }
        
        return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
    }
    
    func imageText(font: UIFont) -> NSAttributedString {
        
        guard let image = image else {
            return NSAttributedString(string: "")
        }
        
        let attachment = EmoticonAttachment()
        attachment.chs = chs
        attachment.image = image
        
        let height = font.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        let attrStrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        attrStrM.addAttributes([NSFontAttributeName: font], range: NSRange(location: 0, length: 1))
        return attrStrM
        
    }
    
    override var description: String{
        return yy_modelDescription()
    }
    
}
